package com.gpframework.module.controller.common;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.common.domain.GpCommonAgreement;
import com.gpframework.module.common.domain.req.GpCommonAgreementAdd;
import com.gpframework.module.common.domain.req.GpCommonAgreementReq;
import com.gpframework.module.common.domain.req.GpCommonAgreementUpdate;
import com.gpframework.module.common.domain.vo.GpCommonAgreementVo;
import com.gpframework.module.common.service.IGpCommonAgreementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName GpCommonAgreementController
 * @Author gpframework
 * @Date 2021-12-09
 * @Version 1.0.0
 * @Description 公共协议1Controller
 */
@Validated
@Api(tags = "公共协议1")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes", "unchecked"})
@RequestMapping("/common/gpCommonAgreement")
public class GpCommonAgreementController extends BaseController {

    private final IGpCommonAgreementService iGpCommonAgreementService;

    /**
     * @Description 分页
     * @Author gpframework
     * @Date 2021-12-09
     * @Param gpCommonAgreement
     * @Return TableDataInfo
     */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('common:gpCommonAgreement:list')")
    @GetMapping("/list")
    public TableDataInfo<GpCommonAgreementVo> list(GpCommonAgreementReq gpCommonAgreementReq) {
        startPage();
        QueryWrapper<GpCommonAgreement> lqw = new QueryWrapper<GpCommonAgreement>();
        lqw.orderByDesc("gp.update_time");
        gpCommonAgreementReq.generatorQuery(lqw, true);
        List<GpCommonAgreementVo> list = iGpCommonAgreementService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2021-12-09
     * @Param gpCommonAgreement
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('common:gpCommonAgreement:add')")
    @Log(title = "添加公共协议1", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpCommonAgreementAdd gpCommonAgreementAdd) {
        GpCommonAgreement gpCommonAgreement = Convert.convert(new TypeReference<GpCommonAgreement>() {
        }, gpCommonAgreementAdd);
        return toAjax(iGpCommonAgreementService.save(gpCommonAgreement) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2021-12-09
     * @Param gpCommonAgreement
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('common:gpCommonAgreement:edit')")
    @Log(title = "修改公共协议1", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GpCommonAgreementUpdate gpCommonAgreementUpdate) {
        GpCommonAgreement gpCommonAgreement = Convert.convert(new TypeReference<GpCommonAgreement>() {
        }, gpCommonAgreementUpdate);
        return toAjax(iGpCommonAgreementService.updateById(gpCommonAgreement) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2021-12-09
     * @Param gpCommonAgreement
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('common:gpCommonAgreement:export')")
    @Log(title = "导出公共协议1", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(GpCommonAgreementReq gpCommonAgreementReq) {
        QueryWrapper<GpCommonAgreement> lqw = new QueryWrapper<GpCommonAgreement>();
        gpCommonAgreementReq.generatorQuery(lqw, true);
        List<GpCommonAgreementVo> list = iGpCommonAgreementService.listVo(lqw);
        ExcelUtil<GpCommonAgreementVo> util = new ExcelUtil<GpCommonAgreementVo>(GpCommonAgreementVo.class);
        return util.exportExcel(list, "gpCommonAgreement");
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2021-12-09
     * @Param gpCommonAgreement
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('common:gpCommonAgreement:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult<GpCommonAgreementVo> getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iGpCommonAgreementService.getVoById(id));
    }


    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2021-12-09
     * @Param gpCommonAgreement
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('common:gpCommonAgreement:remove')")
    @Log(title = "删除公共协议1", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpCommonAgreementService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
